﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using PhotoGallery.Core.Business;
using PhotoGallery.Infrastructure.Helpers;

namespace PhotoGallery.Modules.Authority.Controllers
{
    public class OperationController : Controller
    {

        #region Field & Property


        #endregion

        #region Constructor

        public OperationController()
        {

        }
        #endregion

        #region  操作
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        [LoginFilterAttribute(Order = 0)]
        [SecurityFilterAttribute(Order = 1)]
        public ActionResult Index(OperationSearch search)
        {
            if (TempData["HintMessage"] != null)
            {
                ViewData["HintMessage"] = TempData["HintMessage"];
            }
            OperationIndexModel model = new OperationIndexModel() { Search = search, Operations = Operation.Search(search) };
            return View(model);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        [LoginFilterAttribute(Order = 0)]
        [SecurityFilterAttribute(Order = 1)]
        public ActionResult Add()
        {
            if (TempData["HintMessage"] != null)
            {
                ViewData["HintMessage"] = TempData["HintMessage"];
            }
            return View();
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        [LoginFilterAttribute(Order = 0)]
        [SecurityFilterAttribute(Order = 1)]
        public ActionResult Create(Operation model)
        {
            if (model == null)
                throw new ArgumentNullException("the operation is null!!!");
            model.Save();
            TempData["HintMessage"] = new HintMessage { Content = String.Format("添加（{0}）操作成功！", model.Name) };
            return RedirectToAction("Index");
        }

        [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        [LoginFilterAttribute(Order = 0)]
        [SecurityFilterAttribute(Order = 1)]
        public ActionResult Edit(int? id)
        {
            if (id == null || !id.HasValue)
            {
                throw new ArgumentNullException("the id is null!!!");
            }
            Operation operation = Operation.Load(id.Value);
            if (operation == null)
                throw new ArgumentNullException("请检查是否存在该操作！！");
            return View(operation);
        }


        [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        [LoginFilterAttribute(Order = 0)]
        [SecurityFilterAttribute(Order = 1)]
        public ActionResult Update(Operation model)
        {
            Operation operation = null;
            if (model == null || (operation = Operation.Load(model.Id)) == null)
                throw new ArgumentNullException("请检查输入！！");
            operation.Name = model.Name;
            operation.Controller = model.Controller;
            operation.Action = model.Action;
            operation.Status = model.Status;
            operation.Save();
            TempData["HintMessage"] = new HintMessage { Content = String.Format("修改（{0}）操作成功！", model.Name) };
            return RedirectToAction("Index");
        }


        [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        [LoginFilterAttribute(Order = 0)]
        [SecurityFilterAttribute(Order = 1)]
        public ActionResult Delete(int? id)
        {
            Operation model = null;
            if (id == null || !id.HasValue || (model = Operation.Load(id.Value)) == null)
                throw new ArgumentNullException("请检查是否存在该操作！！");
            model.DeleteOnSave();
            model.Save();
            TempData["HintMessage"] = new HintMessage { Content = String.Format("删除（{0}）操作成功！", model.Name) };
            return RedirectToAction("Index");
        }

        #endregion


        #region Json

        public JsonResult CheckOperation(string controller, string operation)
        {
            if (string.IsNullOrEmpty(controller) || string.IsNullOrEmpty(operation))
            {
                return Json(new { success = false, message = "请确认控制器名称和操作名称！" });
            }
            Operation op = Operation.Find(controller, operation);
            if (op == null)
                return Json(new { success = true, message = "该控制器名称和操作名称可用！", status = "success" });
            else
                return Json(new { success = false, message = "该控制器名称和操作名称已经被占用！", status = "error" });
        }
        #endregion
    }
}
